class Main extends eui.UILayer {
    public static loadingView;
    protected createChildren(): void {
        super.createChildren();
        egret.lifecycle.addLifecycleListener((context) => {
            // custom lifecycle plugin
        })

        //游戏切换到后台
        egret.lifecycle.onPause = () => {
            SoundManage.lock = true;
            SoundManage.stopYinYue();
            egret.MainContext.instance.stage.addEventListener(egret.TouchEvent.TOUCH_TAP, SoundManage.UnlockAudio, this);
            // SocketManager.gameOnPause();
            // egret.ticker.pause();
        }

        //游戏回到前台
        egret.lifecycle.onResume = () => {
            // egret.ticker.resume();
            // SocketManager.gameOnResume();
        }

        this.addChild(GameLayerManager.gameLayer());
        //inject the custom material parser
        //注入自定义的素材解析器
        let assetAdapter = new AssetAdapter();
        egret.registerImplementation("eui.IAssetAdapter", assetAdapter);
        egret.registerImplementation("eui.IThemeAdapter", new ThemeAdapter());
        this.runGame().catch(e => {
            console.log(e);
        })
    }
    private async runGame() {
        //解析资源注册表
        await RES.loadConfig("resource/default.res.json", "resource/");
        await this.loadTheme();
        RES.setMaxLoadingThread(4)
        //加载loading页资源
        let load = new Load()
        egret.MainContext.instance.stage.addChild(load)
        await RES.loadGroup("loading", 0, load);
        egret.MainContext.instance.stage.removeChild(load)
        //html中定义了data-scale-mode="fixWidth" 是为了使html中的加载Log 和Load 中的加载Log尺寸保持一致，但这种适配模式会导致游戏中界面尺寸改变时元素不自动缩放，所以这里在load关闭后 动态修改为FIXED_NARROW
        this.stage.scaleMode = egret.StageScaleMode.FIXED_NARROW;
        //加载游戏大厅页资源
        Main.loadingView = new LoadingUI();
        egret.MainContext.instance.stage.addChild(Main.loadingView);
        await RES.loadGroup("preload", 0, Main.loadingView);
        this.createGameScene();
        //游戏大厅页资源 加载完成后不要马上删除页面，要等登陆返回后打开大厅才删除，否则会看到黑屏一段时间
    }
    private loadTheme() {
        return new Promise((resolve, reject) => {
            //加载皮肤主题配置文件,可以手动修改这个文件。替换默认皮肤。
            let theme = new eui.Theme("resource/default.thm.json", this.stage);
            theme.addEventListener(eui.UIEvent.COMPLETE, () => {
                resolve();
            }, this);
        })
    }
    /**
     * 创建场景界面
     * Create scene interface
     */
    protected createGameScene(): void {
        //游戏初始化
        game.AppFacade.getInstance().startUp(GameLayerManager.gameLayer());
        egret.MainContext.instance.stage.setChildIndex(Main.loadingView, 0)//MVC启动后默认在最底层，需要把loadingView调整到底层去
        egret.MainContext.instance.stage.addEventListener(egret.TouchEvent.TOUCH_TAP, SoundManage.UnlockAudio, this)
        //添加舞台尺寸变化广播事件
        egret.MainContext.instance.stage.addEventListener(egret.Event.RESIZE, () => {
            game.AppFacade.getInstance().sendNotification(PanelNotify.HALL.STAGE_RESIZE)
        }, this)
        SoundManage.changYinLiang();
        Poker_public.init();
        var Request = GetRequest();
        if (Request["username"]) {
            var username: string = Request["username"];
            var platform: string = Request["platform"];
            var token: string = Request["token"];
            var isGuest: string = Request["isGuest"];
            if (Request["apiurl"]) {
                InstructionsEnum.IP_API = Request["apiurl"]
            }
            if (Request["route"]) {
                InstructionsEnum.IP_LOGIN = Request["route"].split(",")[0];
            }
            let obj = {
                dwUserName: isGuest == "0" ? platform + "-" + username : username,
                platform: platform,
                token: token
            }
            InstructionsEnum.UserName = obj.dwUserName
            InstructionsEnum.platForm = obj.platform
            InstructionsEnum.token = obj.token
            InstructionsEnum.isGuest = isGuest
            SocketManager.sendMessage(InstructionsEnum.GAME_TYPE_NAME.HALL, RegDataGame.CMD_MB_LogonUserApi, obj, true);
        } else {
            game.AppFacade.getInstance().sendNotification(PanelNotify.LOGIN.OPEN_MAIN);
        }
    }
}
